package JZOffer;

// miss
public class Day009_2 {
    public static int maxValue(int[][] grid) {
        int ans = grid[0][0];
        int i = 0;
        int j = 0;

        while (true) {
            if (i == grid.length-1 && j == grid[0].length-1) {
                break;
            }
            if (i == grid.length-1) {
                ans += grid[i][j+1];
                j++;
                continue;
            }
            if (j == grid[0].length-1) {
                ans += grid[i+1][j];
                i++;
                continue;
            }
            if (grid[i][j+1] > grid[i+1][j]) {
                ans += grid[i][j+1];
                j++;
            } else {
                ans += grid[i+1][j];
                i++;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        int[][] re = {
                {1,2,5},
                {3,2,1}
        };
        System.out.println(maxValue(re));
    }
}
